﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FTools.Persistencia;
using System.Data.SqlClient;
using System.Data;

namespace FTools.Persistencia
{
    class Conexion
    {
        static SqlConnection conSql;
        
        //ABRIR CONEXION
        public SqlConnection abrirConexion()
        {
            try
            {
                conSql = new SqlConnection();
                conSql.ConnectionString = "Data Source=localhost\\SQLEXPRESS;Initial Catalog=FTools;Integrated Security=True";
                conSql.Open();
                return conSql;
            }
            catch (Exception ex)
            {
                throw new Exception("_Conexion#Persistencia# " + ex.Message);
            }
        }

        //ABRIR CONEXION CON DATOS
        public SqlConnection abrirConexion(String xUser, String xPassword, String xServerName, String xDatabaseName)
        {
            try
            {
                if (xUser != "" && xServerName != "" && xDatabaseName != "")
                {
                    conSql = new SqlConnection();
                    conSql.ConnectionString = "Data Source=" + xServerName + ";Initial Catalog=" + xDatabaseName + ";user=" + xUser + ";password=" + xPassword + ";Integrated Security=True";
                    conSql.Open();
                    return conSql;
                }
                else { throw new Exception("_Conexion#Persistencia# # Datos Incompletos para conexion #"); }
            }
            catch (Exception ex)
            {
                throw new Exception("_Conexion#Persistencia# " + ex.Message);
            }
        }

        //CERRAR CONEXION
        public void cerrarConexion()
        {
            try
            {
                if (conSql != null)
                {
                    conSql.Close();
                }
                else { throw new Exception("_Conexion#Persistencia# # No existe Conexion Abiera #"); }
            }
            catch (Exception ex)
            {
                throw new Exception("_Conexion#Persistencia# " + ex.Message);
            }
        }
    }
}
